﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TicketReservationSystemDataLayer;

namespace WpfApplicationEventsManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TicketReservationSystemDataContext context;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void LoadLocations()
        {
            context = new TicketReservationSystemDataContext();
            dataGridLocations.ItemsSource = LocationDAO.GetAllLocations(context);
            this.buttonEdit.IsEnabled = false;
            this.buttonDelete.IsEnabled = false;
        }

        private void tabItemLocations_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            LoadLocations();
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            AddEditLocationsWindow addLocation = new AddEditLocationsWindow();
            addLocation.Title = "Add Location";
            addLocation.buttonSave.Content = "Add";
            addLocation.ShowDialog();
            if (addLocation.Save)
            {
                LocationDAO.CreateLocation(context, addLocation.textBoxLocation.Text);
            }
            LoadLocations();
        }

        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            AddEditLocationsWindow editLocation = new AddEditLocationsWindow();
            Location locationToEdit = this.dataGridLocations.SelectedItem as Location;
            if (locationToEdit == null)
            {
                return;
            }
            editLocation.Title = "Edit Location";
            editLocation.buttonSave.Content = "Edit";
            editLocation.textBoxLocation.Text = locationToEdit.Name;
            editLocation.ShowDialog();
            if (editLocation.Save)
            {
                LocationDAO.EditLocation(context, locationToEdit, editLocation.textBoxLocation.Text);
            }
            LoadLocations();
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            Location toBeDeleted = this.dataGridLocations.SelectedItem as Location;
            if (toBeDeleted != null)
            {
                LocationDAO.DeleteLocation(context, toBeDeleted);
                LoadLocations();
            }
        }

        private void dataGridLocations_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.buttonEdit.IsEnabled = true;
            this.buttonDelete.IsEnabled = true;
        }

        private void tabItemEvents_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            LoadEvents();
        }

        private void LoadEvents()
        {
            context = new TicketReservationSystemDataContext();
            this.dataGridEvents.ItemsSource = EventDAO.GetAllEvents(context);
            this.buttonDeleteEvent.IsEnabled = false;
            this.buttonEditEvent.IsEnabled = false;
        }

        private void buttonAddEvent_Click(object sender, RoutedEventArgs e)
        {
            AddEditEventsWindow addEvent = new AddEditEventsWindow();
            addEvent.Title = "Add Event";
            addEvent.buttonSave.Content = "Add";
            addEvent.ShowDialog();
            if (addEvent.Save)
            {
                int locationId = ((Location)addEvent.comboBoxLocation.SelectedItem).Id;
                int categoryId = ((Category)addEvent.comboBoxCategory.SelectedItem).Id;
                DateTime date = (DateTime)addEvent.datePickerDAte.SelectedDate;
                decimal price = decimal.Parse(addEvent.textBoxPrice.Text);
                EventDAO.CreateEvent(context, addEvent.textBoxEventName.Text, locationId, categoryId, date, price);
                LoadEvents();
            }
        }

        private void buttonEditEvent_Click(object sender, RoutedEventArgs e)
        {
            context = new TicketReservationSystemDataContext();
            AddEditEventsWindow editEvent = new AddEditEventsWindow();
            Event currentEvent = (Event)this.dataGridEvents.SelectedItem;
            editEvent.Title = "Edit Event";
            editEvent.buttonSave.Content = "Edit";
            editEvent.textBoxEventName.Text = currentEvent.Name;
            editEvent.textBoxPrice.Text = currentEvent.Price.ToString();
            editEvent.datePickerDAte.SelectedDate = currentEvent.DateTime;
            editEvent.Location = currentEvent.Location;
            editEvent.Category = currentEvent.Category;
            editEvent.ShowDialog();
            if (editEvent.Save)
            {
                int locationId = ((Location)editEvent.comboBoxLocation.SelectedItem).Id;
                int categoryId = ((Category)editEvent.comboBoxCategory.SelectedItem).Id;
                DateTime date = (DateTime)editEvent.datePickerDAte.SelectedDate;
                decimal price = decimal.Parse(editEvent.textBoxPrice.Text);
                EventDAO.ModifyEvent(context, EventDAO.FindEventById(context, currentEvent.Id).First(), 
                    editEvent.textBoxEventName.Text, locationId, categoryId, price, date);
                LoadEvents();
            }
        }

        private void buttonDeleteEvent_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dataGridEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.buttonEditEvent.IsEnabled = true;
            this.buttonDeleteEvent.IsEnabled = true;
        }
    }
}
